home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue35 / system / RichEdit2.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1998-06-09  |  7.9 KB  |  255 lines

  1. unit RichEdit2;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ComCtrls, RichEdit;
  8.  
  9. type
  10.     PCharRange = ^TCharRange;
  11.     TURLClickedEvent = procedure (Sender: TObject; const TheURL: String; Button: TMouseButton) of object;
  12.  
  13.     TUndoRedoType = ( uidUnknown, uidTyping, uidDelete, uidDragDrop, uidCut, uidPaste );
  14.  
  15.     TRichEdit2 = class (TCustomRichEdit)
  16.     private
  17.         { Private declarations }
  18.         fLibHandle: THandle;
  19.         fURLHighlight: Boolean;
  20.         fUndoLimit: Integer;
  21.         fURLClicked: TURLClickedEvent;
  22.         fLastCR: TCharRange;
  23.         procedure SetURLHighlight (Value: Boolean);
  24.         function GetRow: Integer;
  25.         function GetColumn: Integer;
  26.         function GetGotSelection: Boolean;
  27.         function GetFirstLine: Integer;
  28.         function GetBoolProp (Index: Integer): Boolean;
  29.         procedure SetUndoLimit (Value: Integer);
  30.         function GetUndoRedoType (Index: Integer): TUndoRedoType;
  31.         procedure WMNCDestroy (var Message: TWMNCDestroy); message wm_NCDestroy;
  32.         procedure CNNotify(var Message: TWMNotify); message cn_Notify;
  33.     protected
  34.         { Protected declarations }
  35.         procedure CreateWnd; override;
  36.         procedure CreateParams (var Params: TCreateParams); override;
  37.         procedure URLLinkNotification (Link: Pointer);
  38.     public
  39.         { Public declarations }
  40.         constructor Create (AOwner: TComponent); override;
  41.     published
  42.         { Published declarations }
  43.         property Align;
  44.         property Alignment;
  45.         property BorderStyle;
  46.         property Color;
  47.         property Ctl3D;
  48.         property DragCursor;
  49.         property DragMode;
  50.         property Enabled;
  51.         property Font;
  52.         property HideSelection;
  53.         property HideScrollBars;
  54.         property ImeMode;
  55.         property ImeName;
  56.         property Lines;
  57.         property MaxLength;
  58.         property ParentColor;
  59.         property ParentCtl3D;
  60.         property ParentFont;
  61.         property ParentShowHint;
  62.         property PlainText;
  63.         property PopupMenu;
  64.         property ReadOnly;
  65.         property ScrollBars;
  66.         property ShowHint;
  67.         property TabOrder;
  68.         property TabStop;
  69.         property Visible;
  70.         property WantTabs;
  71.         property WantReturns;
  72.         property WordWrap;
  73.         property OnChange;
  74.         property OnDragDrop;
  75.         property OnDragOver;
  76.         property OnEndDrag;
  77.         property OnEnter;
  78.         property OnExit;
  79.         property OnKeyDown;
  80.         property OnKeyPress;
  81.         property OnKeyUp;
  82.         property OnMouseDown;
  83.         property OnMouseMove;
  84.         property OnMouseUp;
  85.         property OnResizeRequest;
  86.         property OnSelectionChange;
  87.         property OnStartDrag;
  88.         property OnProtectChange;
  89.         property OnSaveClipboard;
  90.         property URLHighlight: Boolean read fURLHighlight write SetURLHighlight default True;
  91.         property OnURLClicked: TURLClickedEvent read fURLClicked write fURLClicked;
  92.         property GotSelection: Boolean read GetGotSelection;
  93.         property Row: Integer read GetRow;
  94.         property Column: Integer read GetColumn;
  95.         property FirstLine: Integer read GetFirstLine;
  96.         property CanUndo: Boolean index 0 read GetBoolProp;
  97.         property CanRedo: Boolean index 1 read GetBoolProp;
  98.         property UndoLimit: Integer read fUndoLimit write SetUndoLimit default 100;
  99.         property UndoType: TUndoRedoType index 0 read GetUndoRedoType;
  100.         property RedoType: TUndoRedoType index 1 read GetUndoRedoType;
  101.     end;
  102.  
  103. procedure Register;
  104.  
  105. implementation
  106.  
  107. {$R *.DCR}
  108.  
  109. constructor TRichEdit2.Create (AOwner: TComponent);
  110. begin
  111.     Inherited Create (AOwner);
  112.     fUndoLimit := 100;
  113.     fURLHighlight := True;
  114. end;
  115.  
  116. procedure TRichEdit2.CreateWnd;
  117. var
  118.     mask: Integer;
  119. begin
  120.     Inherited CreateWnd;
  121.     mask := Perform (em_GetEventMask, 0, 0) or enm_Link;
  122.     Perform (em_SetEventMask, 0, mask);
  123.     Perform (em_AutoURLDetect, Ord (fURLHighlight), 0);
  124. end;
  125.  
  126. procedure TRichEdit2.SetURLHighlight (Value: Boolean);
  127. begin
  128.     if Value <> fURLHighlight then begin
  129.         fURLHighlight := Value;
  130.         Perform (em_AutoURLDetect, Ord (fURLHighlight), 0);
  131.     end;
  132. end;
  133.  
  134. procedure TRichEdit2.CreateParams (var Params: TCreateParams);
  135. const
  136.     HideScrollBars: array[Boolean] of Longint = (ES_DISABLENOSCROLL, 0);
  137.     HideSelections: array[Boolean] of Longint = (ES_NOHIDESEL, 0);
  138. var
  139.     OldError: Longint;
  140. begin
  141.     OldError := SetErrorMode (sem_NoOpenFileErrorBox);
  142.     fLibHandle := LoadLibrary ('RICHED20.DLL');
  143.     SetErrorMode (OldError);
  144.     if (fLibHandle > 0) and (fLibHandle < hInstance_Error) then fLibHandle := 0;
  145.  
  146.     inherited CreateParams (Params);
  147.     if fLibHandle <> 0 then CreateSubClass (Params, 'RICHEDIT20A')
  148.     else CreateSubClass (Params, 'RICHEDIT');
  149.  
  150.     with Params do
  151.     begin
  152.         Style := Style or HideScrollBars [Inherited HideScrollBars] or HideSelections[HideSelection];
  153.         WindowClass.style := WindowClass.style and not (cs_HRedraw or cs_VRedraw);
  154.     end;
  155. end;
  156.  
  157. procedure TRichEdit2.URLLinkNotification (Link: Pointer);
  158. type
  159.     // Need to redefine this - RICHTEXT.PAS gets it wrong!
  160.     TTextRange = record
  161.         chrg: TCharRange;
  162.         lpstrText: PAnsiChar;
  163.     end;
  164. var
  165.     sz: String;
  166.     TextRange: TTextRange;
  167.     pENLink: ^TENLink absolute Link;
  168. begin
  169.     with pENLink^ do begin
  170.         SetLength (sz, chrg.cpMax - chrg.cpMin);
  171.         TextRange.chrg := chrg;
  172.         TextRange.lpstrText := Pointer (sz);
  173.         Perform (em_GetTextRange, 0, Integer (@TextRange));
  174.         case Msg of
  175.             wm_MouseMove:         ;
  176.             wm_LButtonDown:  if Assigned (fURLClicked) then fURLClicked (Self, sz, mbLeft);
  177.             wm_MButtonDown:  if Assigned (fURLClicked) then fURLClicked (Self, sz, mbMiddle);
  178.             wm_RButtonDown:  if Assigned (fURLClicked) then fURLClicked (Self, sz, mbRight);
  179.         end;
  180.     end;
  181. end;
  182.  
  183. procedure TRichEdit2.CNNotify (var Message: TWMNotify);
  184. begin
  185.     if Message.NMHdr^.Code <> en_Link then Inherited else URLLinkNotification (Message.NMHdr);
  186. end;
  187.  
  188. procedure TRichEdit2.WMNCDestroy (var Message: TWMNCDestroy);
  189. begin
  190.     Inherited;
  191.     if fLibHandle <> 0 then FreeLibrary (fLibHandle);
  192. end;
  193.  
  194. function TRichEdit2.GetGotSelection: Boolean;
  195. begin
  196.     Perform (em_ExGetSel, 0, Integer (@fLastCR));
  197.     Result := fLastCR.cpMin <> fLastCR.cpMax;
  198. end;
  199.  
  200. function TRichEdit2.GetRow: Integer;
  201. var
  202.     cp: Integer;
  203. begin
  204.     cp := -1;
  205.     if GetGotSelection then cp := fLastCR.cpMin;
  206.     Result := Perform (em_LineFromChar, cp, 0) + 1;
  207. end;
  208.  
  209. function TRichEdit2.GetColumn: Integer;
  210. var
  211.     lp: Integer;
  212. begin
  213.     lp := Perform (em_LineIndex, -1, 0);
  214.     if GetGotSelection then lp := Perform (em_LineIndex, Perform (em_ExLineFromChar, 0, fLastCR.cpMin), 0);
  215.     Result := fLastCR.cpMin - lp + 1;
  216. end;
  217.  
  218. function TRichEdit2.GetFirstLine: Integer;
  219. begin
  220.     Result := Perform (em_GetFirstVisibleLine, 0, 0);
  221. end;
  222.  
  223. function TRichEdit2.GetBoolProp (Index: Integer): Boolean;
  224. begin
  225.     Result := False;  { Stop compiler whinging }
  226.     case Index of
  227.         0:     Result := Perform (em_CanUndo, 0, 0) <> 0;
  228.         1:     Result := Perform (em_CanRedo, 0, 0) <> 0;
  229.     end;
  230. end;
  231.  
  232. procedure TRichEdit2.SetUndoLimit (Value: Integer);
  233. begin
  234.     if (fUndoLimit <> Value) and (Value >= 10) and (Value <= 400) then begin
  235.         fUndoLimit := Value;
  236.         Perform (em_SetUndoLimit, Value, 0);
  237.     end;
  238. end;
  239.  
  240. function TRichEdit2.GetUndoRedoType (Index: Integer): TUndoRedoType;
  241. begin
  242.     Result := uidUnknown;  { Stop compiler whinging }
  243.     case Index of
  244.         0:     Result := TUndoRedoType (Perform (em_GetUndoName, 0, 0));
  245.         1:     Result := TUndoRedoType (Perform (em_GetRedoName, 0, 0));
  246.     end;
  247. end;
  248.  
  249. procedure Register;
  250. begin
  251.   RegisterComponents('XFactor', [TRichEdit2]);
  252. end;
  253.  
  254. end.
  255.